﻿
Public Class FrmUserManage
    Private FUid As String
    Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        If DataGridView1.SelectedCells.Count < 1 Then
            Return
        End If
        Try
            FUid = DataGridView1.Item(0, e.RowIndex).Value
            If FUid = Nothing Then
                Return
            End If
            TextBox1.Text = DataGridView1.Item(1, e.RowIndex).Value
            TextBox2.Text = DataGridView1.Item(2, e.RowIndex).Value
            TextBox3.Text = DataGridView1.Item(3, e.RowIndex).Value

            For i As Integer = 0 To ComboBox1.Items.Count - 1
                If ComboBox1.GetItemText(ComboBox1.Items(i)) = DataGridView1.Item(4, e.RowIndex).Value Then
                    ComboBox1.SelectedIndex = i
                    Exit For
                End If
            Next
        Catch ex As Exception
            Return
        End Try
    End Sub

    Private Sub FrmUserManage_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        GetUserList()
    End Sub
    Private Sub GetUserList()
        Try
            Dim ErrString As String = String.Empty
            Dim ds As New DataSet
            Dim tmpArr As Byte() = Nothing
            DataGridView1.DataSource = Nothing
            If SControl._BASE_User_GetUserList(UInfo.FSession, "", tmpArr, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            If FControl.DsRestore(ds, tmpArr) = False Then
                MsgBox("无法解析返回数据!")
                Return
            End If
            DataGridView1.DataSource = ds.Tables(0).DefaultView
            ds = New DataSet
            tmpArr = Nothing
            ErrString = Nothing
            If SControl._BASE_Org_GetOrgList(UInfo.FSession, "*", tmpArr, ErrString) = False Then
                MsgBox(String.Format("读取单位列表失败!{0}错误信息:{1}", vbCrLf, ErrString))
                Return
            End If
            If FControl.DsRestore(ds, tmpArr) = False Then
                MsgBox("无法解析返回数据!")
                Return
            End If
            ComboBox1.DataSource = ds.Tables(0).DefaultView
            ComboBox1.DisplayMember = "FName"
            ComboBox1.ValueMember = "id"
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ButtonX1_Click(sender As Object, e As EventArgs) Handles ButtonX1.Click
        If TextBox1.Text.Trim = String.Empty Then
            MsgBox("用户名不能为空!")
            Return
        End If
        If TextBox3.Text.Trim = String.Empty Then
            MsgBox("姓名不能为空!")
            Return
        End If
        If ComboBox1.SelectedValue < 0 Then
            MsgBox("单位必须选择!")
            Return
        End If
        Try
            Dim ErrString As String = String.Empty
            Dim FUinfo As New BServer.FUserInfo
            FUinfo.FName = TextBox1.Text.Trim
            FUinfo.FPass = TextBox2.Text.Trim
            FUinfo.FRName = TextBox3.Text.Trim
            FUinfo.FOrgid = ComboBox1.SelectedValue
            If SControl._Base_User_AddNew(UInfo.FSession, FUinfo, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            GetUserList()
            FUid = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ButtonX2_Click(sender As Object, e As EventArgs) Handles ButtonX2.Click
        If FUid = String.Empty Then
            MsgBox("必须先选择一个要修改的用户")
            Return
        End If
        If TextBox1.Text.Trim = String.Empty Then
            MsgBox("用户名不能为空!")
            Return
        End If
        If TextBox3.Text.Trim = String.Empty Then
            MsgBox("姓名不能为空!")
            Return
        End If
        If ComboBox1.SelectedValue < 0 Then
            MsgBox("单位必须选择!")
            Return
        End If
        Try
            Dim ErrString As String = String.Empty
            Dim FUinfo As New BServer.FUserInfo With {.id = FUid, .FName = TextBox1.Text.Trim, .FPass = TextBox2.Text, .FRName = TextBox3.Text, .FOrgid = ComboBox1.SelectedValue}
            If SControl._Base_User_ModifyUser(UInfo.FSession, FUinfo, CheckBox1.Checked, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            GetUserList()
            FUid = Nothing
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ButtonX3_Click(sender As Object, e As EventArgs) Handles ButtonX3.Click
        If FUid = Nothing Then
            MsgBox("必须选择一个要删除的用户!")
            Return
        End If
        Try
            If MsgBox("用户删除后不可恢复,由此用户填写的所有单据查询时有可能出错,是否继续删除?", MsgBoxStyle.YesNo, "删除用户") = MsgBoxResult.No Then
                Return
            End If
            '--删除
            Dim ErrString As String = Nothing
            If SControl._Base_User_Delete(UInfo.FSession, FUid, ErrString) = False Then
                MsgBox(ErrString)
                Return
            End If
            MsgBox("删除成功!")
            GetUserList()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class